from common.search import proxy_map
from package.connector.elastic_db import es_db


class PlatSearch:
    """平台搜索"""
    def __init__(self, es):
        self.es = es

    def plat_search(self, search, page, page_size):
        index = 'dsm-69d2075405fd11f080e0cad3ba39292f-000001'
        body = search['search']
        return self.es.search(index='dsm-69d2075405fd11f080e0cad3ba39292f-000001', body=body)


class ProxySearch:
    """代理搜索"""

    @staticmethod
    def _get_proxy(proxy_configure):
        db_type = proxy_configure.pop('category')
        cls = proxy_map.get(db_type)
        return cls
        return cls(**proxy_configure['configure'], host=proxy_configure['host'], port=proxy_configure['port'])

    def search(self, proxy_configure, search_configure):
        cls = self._get_proxy(proxy_configure)
        proxy_ = cls(configure=dict(**proxy_configure['configure'], host=proxy_configure['host'], port=proxy_configure['port']),
                     condition=search_configure
                     )
        resp = proxy_.search()
        return [dict(row._mapping) for row in resp]


plat = PlatSearch(es=es_db)
proxy = ProxySearch()
